//
//  StarView.m
//  HWSGW
//
//  Created by avachen on 15/11/2.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "StarView.h"
#import "UIViewExt.h"

@implementation StarView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _createView];
    }
    return self;
}
- (void)awakeFromNib
{
    [self _createView];
}
//创建星星视图
- (void)_createView{
    UIImage *yellowImg = [UIImage imageNamed:@"yellow"];
    UIImage *grayImg = [UIImage imageNamed:@"gray"];
    
    //1、创建灰色星星
    _grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, grayImg.size.width * 5, grayImg.size.height)];
    _grayView.backgroundColor = [UIColor colorWithPatternImage:grayImg];
    [self addSubview:_grayView];
    
    //2、创建金色星星
    _yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, yellowImg.size.width * 5, yellowImg.size.height)];
    _yellowView.backgroundColor = [UIColor colorWithPatternImage:yellowImg];
    [self addSubview:_yellowView];
    
    //给当前的视图一个背景
    self.backgroundColor = [UIColor clearColor];
    //3、设置当前的视图的尺寸
    
    
    //因为星星的高和宽是一样的，所以父视图的宽可以用高的5倍来设置
    CGFloat starWidth =  self.frame.size.height * 5;
    self.width = starWidth;
    
    
    
    //星星放大的尺寸就是父视图view的高度/星星高度
    CGFloat scale =  self.frame.size.height / yellowImg.size.height;
    
   
  
    
    //4、放大星星
    _grayView.transform = CGAffineTransformMakeScale(scale, scale);
    _yellowView.transform = CGAffineTransformMakeScale(scale, scale);
    
    //星星放大之后位置坐标会偏移，所以需要把位置重新调整，因为星星视图在父视图上，原始坐标为（0， 0）
    _yellowView.origin = CGPointZero;
    _grayView.origin = CGPointZero;
    
    
}
- (void)setRating:(float)rating
{
    _rating = rating;
    CGFloat s = rating / 10.0;
    CGFloat width = self.frame.size.width * s;
    //修改yellowView的width值
//    CGRect frame = _yellowView.frame;
//    frame.size.width = width;
//    _yellowView.frame = frame;
    _yellowView.width = width;
    
//等价于  _yellowView.frame.size.width = width;
    
}
@end
